/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import tpdisenio2009.model.beans.FantasyTeam;
import tpdisenio2009.model.beans.FantasyTeamPlayer;
import tpdisenio2009.model.beans.FantasyTeamScoreByTournamentDate;
import tpdisenio2009.model.beans.Score;
import tpdisenio2009.model.beans.SoccerPlayer;
import tpdisenio2009.model.beans.TournamentDate;
import tpdisenio2009.model.dao.FantasyTeamPlayerDAO;
import tpdisenio2009.model.dao.ScoreDAO;

/**
 *
 * @author DeuterioX
 */
public class FantasyTeamPlayerService {

    private final FantasyTeamPlayerDAO fantasyTeamPlayerDao;
    private final ScoreDAO scoreDAO;


    public FantasyTeamPlayerService() {

        fantasyTeamPlayerDao = new FantasyTeamPlayerDAO();
        scoreDAO = new ScoreDAO();
    }

    public List<FantasyTeamPlayer> getFantasyTeamPlayersByDate( FantasyTeam team, TournamentDate date ) {
        return fantasyTeamPlayerDao.getFantasyTeamsPlayersByDate( team, date );
    }

    public Vector<Vector<String>> getTeamWithScoreAndPosition( FantasyTeam team, TournamentDate date ) {        

        List<FantasyTeamPlayer> players = fantasyTeamPlayerDao.getFantasyTeamsPlayersByDate( team, date );        

        Vector<Vector<String>> matrix = new Vector<Vector<String>>();
        matrix.setSize(15);
        Integer defender = 1, midfielder = 1, forward = 1;
        int gk = 0, df = 0, mf = 0, fw = 0;
        for ( FantasyTeamPlayer player : players ) {
            Vector<String> values = new Vector<String>();
            if ( player.getSoccerPlayer().getPosition().equals( SoccerPlayer.DEFENDER_POSITION ) ) {
                if ( player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) ) {
                    values.add( player.getSoccerPlayer().getPosition() + " " + String.valueOf(defender) );
                    defender++;
                } else {
                    values.add( player.getSoccerPlayer().getPosition() + " Suplente" );
                }
            } else if ( player.getSoccerPlayer().getPosition().equals( SoccerPlayer.MIDFIELDER_POSITION ) ) {
                if ( player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) ) {
                    values.add( player.getSoccerPlayer().getPosition() + " " + String.valueOf(midfielder) );
                    midfielder++;
                } else {
                    values.add( player.getSoccerPlayer().getPosition() + " Suplente" );
                }
            } else if ( player.getSoccerPlayer().getPosition().equals( SoccerPlayer.FORWARD_POSITION ) ) {
                if ( player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) ) {
                    values.add( player.getSoccerPlayer().getPosition() + " " + String.valueOf(forward) );
                    forward++;
                } else {
                    values.add( player.getSoccerPlayer().getPosition() + " Suplente" );
                }
            } else if ( player.getSoccerPlayer().getPosition().equals( SoccerPlayer.GOALKEEPER_POSITION ) ) {
                values.add( player.getSoccerPlayer().getPosition() + (( player.getRol().equals( FantasyTeamPlayer.SUBSTITUTE_ROLE ) )? " Suplente" : "" ) );                
            }
            values.add( player.getSoccerPlayer().getLastName() + ", " + player.getSoccerPlayer().getFirstName() );
            values.add( player.getSoccerPlayer().getRealTeam().getName() );
            values.add( String.valueOf( player.getSoccerPlayer().getPrice() ) );           

            for ( Score score : player.getSoccerPlayer().getScoreCollection() ) {
                if ( (score.getTournamentDate().equals( date )) ) {
                    if( score.getSoccerPlayer().getPosition().equals(SoccerPlayer.GOALKEEPER_POSITION) && player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) ) {
                        gk++;
                    } else if ( score.getSoccerPlayer().getPosition().equals(SoccerPlayer.DEFENDER_POSITION) && player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) ) {
                        df++;
                    } else if ( score.getSoccerPlayer().getPosition().equals(SoccerPlayer.MIDFIELDER_POSITION) && player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) ) {
                        mf++;
                    } else if ( score.getSoccerPlayer().getPosition().equals(SoccerPlayer.FORWARD_POSITION) && player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) ) {
                        fw++;
                    }
                }
            }

            if ( team.getCreationDate().before( date.getBanDate() ) ) {
                for ( Score score : player.getSoccerPlayer().getScoreCollection() ) {
                    if ( (score.getTournamentDate().equals( date )) ) {
                        if ( player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) ) {
                            values.add( String.valueOf( score.getTotalScore() ) );
                            break;
                        } else {
                            if ( gk == 0 && player.getRol().equals(FantasyTeamPlayer.SUBSTITUTE_ROLE) && player.getSoccerPlayer().getPosition().equals(SoccerPlayer.GOALKEEPER_POSITION) ) {
                                values.add( String.valueOf( score.getTotalScore() ) );
                                break;
                            } else if ( df <= 3 && player.getRol().equals(FantasyTeamPlayer.SUBSTITUTE_ROLE) && player.getSoccerPlayer().getPosition().equals(SoccerPlayer.DEFENDER_POSITION) ) {
                                values.add( String.valueOf( score.getTotalScore() ) );
                                break;
                            } else if ( mf < 3 && player.getRol().equals(FantasyTeamPlayer.SUBSTITUTE_ROLE) && player.getSoccerPlayer().getPosition().equals(SoccerPlayer.MIDFIELDER_POSITION) ) {
                                values.add( String.valueOf( score.getTotalScore() ) );
                                break;
                            } else if ( fw < 3 && player.getRol().equals(FantasyTeamPlayer.SUBSTITUTE_ROLE) && player.getSoccerPlayer().getPosition().equals(SoccerPlayer.FORWARD_POSITION) ) {
                                values.add( String.valueOf( score.getTotalScore() ) );
                                break;
                            } else {
                                values.add("");
                                break;
                            }
                        }
                    }
                }
            }
            
            if ( player.getSoccerPlayer().getPosition().equals( SoccerPlayer.GOALKEEPER_POSITION ) ) {
                    if ( player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) )
                        matrix.set(0, values);
                    else
                        matrix.set(11, values);
            } else if ( player.getSoccerPlayer().getPosition().equals( SoccerPlayer.DEFENDER_POSITION ) ) {
                    if ( player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) )
                        matrix.set(defender-1, values);
                    else
                        matrix.set(12, values);
            } else if ( player.getSoccerPlayer().getPosition().equals( SoccerPlayer.MIDFIELDER_POSITION ) ) {
                    if ( player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) )
                        matrix.set(midfielder+3, values);
                    else
                        matrix.set(13, values);
            } else if ( player.getSoccerPlayer().getPosition().equals( SoccerPlayer.FORWARD_POSITION ) ) {
                    if ( player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) )
                        matrix.set(forward+6, values);
                    else
                        matrix.set(14, values);
            } else {
                // No se agrega nada.                
            }            
        }        
        return matrix;
    }
    
    public List<FantasyTeamPlayer> getBestPlayers( List<FantasyTeamScoreByTournamentDate> fantasyTeamScores, TournamentDate date ) {
        List<FantasyTeamPlayer> bestPlayers = new ArrayList<FantasyTeamPlayer>();
        FantasyTeamPlayer bestPlayer = null;
        int bestScore = 0;

        for( FantasyTeamScoreByTournamentDate fantasyTeamScore : fantasyTeamScores ) {
            List<FantasyTeamPlayer> players = fantasyTeamPlayerDao.getFantasyTeamsPlayersByDate( fantasyTeamScore.getFantasyTeam(), date );
                        
            for( FantasyTeamPlayer player : players ) {
                if ( player.getRol().equals(FantasyTeamPlayer.TITULAR_ROLE) ) {
                    Score score = scoreDAO.getScoreByTournamentDate(date, player.getSoccerPlayer() );
                    if ( ( score != null ) && ( score.getTotalScore() > bestScore ) ) {
                        bestScore = score.getTotalScore();
                        bestPlayer = player;
                    }
                }                
            }
            bestScore = 0;
            bestPlayers.add(bestPlayer);
            bestPlayer = null;
        }
        return bestPlayers;
    }
    
    

    public boolean validateGoalkeepers( List<SoccerPlayer> players, SoccerPlayer selectedPlayer ) {
        int count = 0;

        for( SoccerPlayer player : players ) {
            if ( player.getPosition().equals(SoccerPlayer.GOALKEEPER_POSITION) && player.getPosition().equals( selectedPlayer.getPosition() ) ) {
                count++;
            }
        }

        if ( count >= 2 ) {
            return true;
        } else {
            return false;
        }
    }

    public boolean validateDefenders( List<SoccerPlayer> players, SoccerPlayer selectedPlayer ) {
        int count = 0;

        for( SoccerPlayer player : players ) {
            if ( player.getPosition().equals(SoccerPlayer.DEFENDER_POSITION) && player.getPosition().equals( selectedPlayer.getPosition() ) ) {
                count++;
            }
        }

        if ( count >= 5 ) {
            return true;
        } else {
            return false;
        }
    }

    public boolean validateMidfielers( List<SoccerPlayer> players, SoccerPlayer selectedPlayer ) {
        int count = 0;

        for( SoccerPlayer player : players ) {
            if ( player.getPosition().equals(SoccerPlayer.MIDFIELDER_POSITION) && player.getPosition().equals( selectedPlayer.getPosition() ) ) {
                count++;
            }
        }

        if ( count >= 4 ) {
            return true;
        } else {
            return false;
        }
    }

    public boolean validateFordwars( List<SoccerPlayer> players,SoccerPlayer selectedPlayer ) {
        int count = 0;

        for( SoccerPlayer player : players ) {
            if ( player.getPosition().equals(SoccerPlayer.FORWARD_POSITION) && player.getPosition().equals( selectedPlayer.getPosition() ) ) {
                count++;
            }
        }

        if ( count >= 4 ) {
            return true;
        } else {
            return false;
        }
    }

    public boolean validateSameTeamPlayers( List<SoccerPlayer> players, SoccerPlayer soccerPlayer ) {
        int count = 0;

        for( SoccerPlayer player : players ) {
            if ( player.getRealTeam().equals( soccerPlayer.getRealTeam() ) ) {
                count++;
            }
        }

        if ( count >= 3 ) {
            return true;
        } else {
            return false;
        }
    }

    public boolean validateSamePlayer( List<SoccerPlayer> players, SoccerPlayer soccerPlayer ) {
        
        for( SoccerPlayer player : players ) {
            if ( player.equals( soccerPlayer ) ) {
                return true;
            }
        }
        return false;
    }

    public boolean existsCurrentPlayerInFantasyTeam( FantasyTeam team, TournamentDate date, SoccerPlayer soccerPlayer ) {
        List<FantasyTeamPlayer> players = fantasyTeamPlayerDao.getFantasyTeamsPlayersByDate(team, date);

        for ( FantasyTeamPlayer player : players ) {
            if ( player.getSoccerPlayer().equals(soccerPlayer) ) {
                return true;
            }
        }
        return false;
    }

    public void setEndDate(SoccerPlayer player) {
        fantasyTeamPlayerDao.setEndDate( player );
    }

    public void updatePlayers(List<SoccerPlayer> players) {
        fantasyTeamPlayerDao.updatePlayers( players );
    }

    public void updateLineUpPlayers(List<SoccerPlayer> players) {
        fantasyTeamPlayerDao.updateLineUpPlayers( players );
    }

    public void updateSubstitutePlayers(List<SoccerPlayer> players) {
        fantasyTeamPlayerDao.updateSubstitutePlayers( players );
    }

    public void updateSwapPlayers( FantasyTeamPlayer player, FantasyTeamPlayer substitute ) {
        fantasyTeamPlayerDao.updateSwapPlayers(player, substitute);
    }

}
